Reverse Integer

Reverse digits of an integer.
Example1: x = 123, return 321 Example2: x = -123, return -321

题目大意:翻转整数,123 => 321,-123 => -321

题目难度:Easy

/**
 * Created by gzdaijie on 16/5/4
 * 翻转,求余进位即可,处理溢出的情况
 */
public class Solution {
    public int reverse(int x) {
        long result = 0;
        while (x != 0) {
            result = result * 10 + x % 10;
            if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
                return 0;
            }
            x /= 10;
        }
        return (int)result;
    }
}
gzdaijie            updated 2016-05-04 22:57:30

results matching ""

    No results matching ""